{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plot Actions\n",
    "\n",
    "Plots can be configured to run code or other cells when the user clicks on or types into them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from beakerx import *\n",
    "from random import randint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "abc = 0 # test variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = Plot(showLegend = True, useToolTip= False)\n",
    "\n",
    "def on_click1(info):\n",
    "    info.graphics.display_name = \"new name\"\n",
    "    \n",
    "def on_click2(info):\n",
    "    info.graphics.y[0] = randint(0, 9)\n",
    "    \n",
    "p.add(Line(x = [1, 2, 3], y = [2, 3, 4], width = 10, displayName = \"line 1\").onClick(on_click1))\n",
    "p.add(Line(x = [1, 2, 3], y = [5, 6, 7], width = 10, displayName = \"line 2\").onClick(on_click2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = Plot(showLegend = True, useToolTip = False)\n",
    "\n",
    "def on_click(info):\n",
    "    global abc\n",
    "    abc += 1\n",
    "    beakerx.runByTag(\"on_click_any_action\")\n",
    "    \n",
    "\n",
    "p.add(Line(x = [1, 2, 3], y = [2, 3, 4], width = 10, displayName = \"line 1\").onClick(on_click))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "on_click_any_action"
    ]
   },
   "outputs": [],
   "source": [
    "print(abc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot = Plot(useToolTip = False)\n",
    "plot.add(Points(x = list(range(1, 6)), y = list(range(1, 6)), size = 12, color = Color.orange, outlineColor = Color.black, displayName = \"orange\").onClick(\"run_tag\"))\n",
    "plot.add(Points(x = list(range(1, 6)), y = list(range(3, 9)), size = 12, color = Color.green, outlineColor = Color.black, displayName = \"green\").onClick(\"run_tag\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "run_tag"
    ]
   },
   "outputs": [],
   "source": [
    "details = plot.details\n",
    "item = details.graphics\n",
    "index = details.index\n",
    "key = details.key\n",
    "tag = details.tag\n",
    "action = details.actionType\n",
    "print(\"You clicked on {} {} (element with coordinates [{},{}])\".format(item.display_name, type(item).__name__, item.x[index], item.y[index]))\n",
    "print(\"Key pressed = {} Tag = {} Action = {}\".format(key, tag, action))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "barsPlot = Plot(useToolTip = False);\n",
    "bars = Bars(x = list(range(1,6)), y = [5, 2, 4, 3, 7], color = Color.green, outlineColor = Color.black, width = 0.3)\n",
    "\n",
    "\n",
    "def on_space(info):\n",
    "    info.graphics.y[info.index] += 1\n",
    "    \n",
    "def on_caps_lock(info):\n",
    "    info.graphics.y[info.index] -= 1\n",
    "\n",
    "#Also buttons like KeyboardCodes.UP_ARROW is handled by jupyter notebook\n",
    "bars.onKey(KeyboardCodes.SPACE, on_space)\n",
    "\n",
    "#Also buttons like KeyboardCodes.DOWN_ARROW is handled by jupyter notebook\n",
    "bars.onKey(KeyboardCodes.CAPS_LOCK, on_caps_lock)\n",
    "        \n",
    "#Tag events working\n",
    "bars.onKey(\"T\", \"run_tag2\")\n",
    "\n",
    "barsPlot.add(bars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "run_tag2"
    ]
   },
   "outputs": [],
   "source": [
    "details = barsPlot.details\n",
    "item = details.graphics\n",
    "index = details.index\n",
    "key = details.key\n",
    "tag = details.tag\n",
    "action = details.actionType\n",
    "print(\"Key action on {} (element with coordinates [{}, {}])\".format(type(item).__name__, item.x[index], item.y[index]))\n",
    "print(\"Key pressed  = {} Tag = {} Action = {}\".format(key, tag, action))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "barsPlot = Plot(useToolTip = False);\n",
    "bars = Bars(x = list(range(1,5)), y = [5, 2, 4, 3, 7], color = Color.green, outlineColor = Color.black, width = 0.3)\n",
    "        \n",
    "#Buttons like KeyboardCodes.UP_ARROW is handled by jupyter notebook\n",
    "def on_space(info):\n",
    "    global abc\n",
    "    abc+=1\n",
    "    beakerx.runByTag('run_tag3')\n",
    "    \n",
    "bars.onKey(KeyboardCodes.SPACE, on_space)\n",
    "barsPlot.add(bars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "run_tag3"
    ]
   },
   "outputs": [],
   "source": [
    "print(abc)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": false,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": false,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
